package Runde4;

public class PreorderTree extends SortedTree {
	
	/**
	 * @precondition root.length() > 0 and root != NULL
	 * @postcondition Creates a new binary search tree with the given root node, labeled with the given name.
	 * @param root The label of the root node.
	 * @throws InvalidParamException If the preconditions are not met.
	 */
	public PreorderTree(String root) throws InvalidParamException {
		super(root);
	}
	
	/**
	 * @postcondition Returns a string containing the output of the preorder traversion.
	 */
	public String traverse() {
		return traverseOutput(root);
	}
	
	/**
	 * Auxiliary method for traverse(), therefore has the same postconditions.
	 * @param node The root of the tree to traverse.
	 */
	private String traverseOutput(Node node) {
		String output = node.toString() + " ";
		
		if (!(node.getLeftChild() == null)) {
			output += traverseOutput(node.getLeftChild());
		}
		
		if (!(node.getRightChild() == null)) {
			output += traverseOutput(node.getRightChild());
		}
		
		return output;
	}

}